# -*- coding: utf-8 -*-

from odoo import models, fields, api, modules, _
from odoo.exceptions import UserError

class Province(models.Model):
    _name = 'mxbase.province'

    name = fields.Char(required=True, string=u'名称')
    code = fields.Char(required=True, string=u'编码')
    weather_city_id = fields.Char(u'墨迹天气城市ID')
    weather_cn_id = fields.Char(u'天气城市ID')

    capital = fields.Boolean(string=u'首都')
    city_province = fields.Boolean(string=u'直辖市')
    special_province = fields.Boolean(string=u'特别行政区')
    no_city = fields.Boolean(compute='_compute_no_city', store=False, string=u'没有地级市')

    central_city_id = fields.Many2one('mxbase.city', string=u'省会城市')

    county_ids = fields.One2many('mxbase.county', 'province_id', string=u'区县')
    city_ids = fields.One2many('mxbase.city', 'province_id', string=u'市')

    @api.model
    def _compute_no_city(self):
        for province in self:
            if province.special_province or province.city_province:
                province.no_city = True
            else:
                province.no_city = False

    @api.model
    def get_central_city(self):
        '''返回省会城市 直辖市'''
        central_city_ids = []
        city_province_ids = []
        for province in self.search([]):
            if province.central_city_id:
                central_city_ids.append(province.central_city_id.id)
            elif province.city_province or province.special_province:
                city_province_ids.append(province.id)
        return {
            'central_city_ids': central_city_ids,
            'city_province_ids': city_province_ids,
        }

    @api.model
    def get_all_province(self):
        '''返回所有省'''
        return self.search([])

